<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class Utils_model extends AppModel{

    function Utils_model($id=NULL){
        parent::AppModel($id);
        $CI = &get_instance();
    }
    /*-----------------------------------------------------------------------------------------------
    get_tid()
    returns the term_id that relates to the current term
    -----------------------------------------------------------------------------------------------*/
    function get_tid($admin_id=NULL) {
        $CI = &get_instance();
        $CI->db->select('t.term_id AS termID');
        $CI->db->where('t.current', 'YES');
        $CI->db->where('t.admin_id', $this->admin_id);
        $results = $CI->db->get(TERM.' t');
        if ($results->num_rows() > 0){
           $row = $results->row_array();
           return $row['termID'];
        }else{
            $CI->db->select('t.term_id AS termID');
            $CI->db->where('t.admin_id', $this->admin_id);
            $CI->db->limit(1);
            $results1 = $CI->db->get(TERM.' t');
            if($results1->num_rows() > 0){
                $row1 = $results1->row_array();
                return $row1['termID'];
            }
        }
        return 0 ;
    }
	/*-----------------------------------------------------------------------------------------------
		getAge()	// This will return age
	-----------------------------------------------------------------------------------------------*/	
	function getAge($iMonth, $iDay, $iYear) {
		$iTimeStamp = mktime() - 86400 - mktime(0, 0, 0, $iMonth, $iDay, $iYear);
    	$iDays = $iTimeStamp / 86400;
		$iMonths = floor($iDays / 30);
		return $iMonths;
	}
	/*-----------------------------------------------------------------------------------------------
		getParents()	// This will return parents/guardian 
	-----------------------------------------------------------------------------------------------*/	
	function getParents($mother, $father) {
		if($father != "" && $mother != ""){ 
				$parents = $father.' and '.$mother;
		}elseif($father != ""){
				$parents = $father;
		}elseif($mother != ""){
				$parents = $mother;
		}else{
				$parents = 'No Guardian on Records';
		}
		return $parents;	
	}
    /*-----------------------------------------------------------------------------------------------
    get_class_name()
    returns name of a class
    -----------------------------------------------------------------------------------------------*/
    function get_class_name($cid, $clid){
        $this->db->select('p.'.PRODUCT_NAME.' AS prod_name');
        $this->db->select('l.'.LOCATION_NAME.' AS loc_name');
        $this->db->select('c.'.COURSE_INSTANCE_DAY.' AS day, c.'.COURSE_INSTANCE_START_TIME.' AS start_time');
        $this->db->from(PRODUCT.' p');
        $this->db->join(COURSE_INSTANCE.' c', 'p.'.PRODUCT_FRANCHISE_ID.' = c.'.COURSE_INSTANCE_FRANCHISE_ID.' AND p.'.PRODUCT_PRODUCT_ID.' = c.'.COURSE_INSTANCE_PRODUCT_ID);
        $this->db->join(LOCATION.' l', 'p.'.PRODUCT_FRANCHISE_ID.' = l.'.LOCATION_FRANCHISE_ID.' AND c.'.COURSE_INSTANCE_LOCATION_ID.' = l.'.LOCATION_LOCATION_ID);
        $this->db->where('p.'.PRODUCT_FRANCHISE_ID, $this->admin_id);
        $this->db->where('p.'.PRODUCT_PRODUCT_ID, $cid);
        $this->db->where('c.'.COURSE_INSTANCE_CLASS_ID, $clid);
        $res = $this->db->get();
        if($res->num_rows()>0){
            $row = $res->row();
            $start_time = date('h:i', strtotime($row->start_time));
            return $row->loc_name." : ".$row->prod_name." - ".$row->day.", ".$start_time;
        }else
            return '';
    }
	/*-----------------------------------------------------------------------------------------------
    get_term_price()
    returns name of a class
    -----------------------------------------------------------------------------------------------*/
    function get_term_price($term_id, $course_id){
        $CI = &get_instance();
        $CI->load->module_model('administrator.prices.price');
        return $CI->price->get_current_price($term_id, $course_id);
        /*$this->db->select(PRICE_LIST_BABY.' AS price');
        $this->db->from(PRICE_LIST);
        $this->db->where(PRICE_LIST_FRANCHISE_ID, $this->admin_id);
        $this->db->where(PRICE_LIST_TERM_ID, $term_id);
		$this->db->where(PRICE_LIST_WEEK_NUM, 0);

        $res = $this->db->get();
        if($res->num_rows()>0){
            $row = $res->row();
            return $row->price;
        }else
            return '';*/
    }
    
    /*-----------------------------------------------------------------------------------------------
    get_makeup()
    returns the value of make up class YES/NO
    -----------------------------------------------------------------------------------------------*/
    function get_makeup($admin_id=NULL) {
        $CI = &get_instance();
        $CI->load->module_model('configurarions.config_model');
        $row = $CI->config_model->get_config('make_up_on_red');
        return $row?$row->value:'';
    }
    
    /*-----------------------------------------------------------------------------------------------
    get_term_name()
    returns the name (year term) of a term_id
    -----------------------------------------------------------------------------------------------*/
    function get_term_name($fid, $tid) {
        $this->db->select('t.'.TERM_TERM.' AS term, t.'.TERM_YEAR.' AS year' );
        $this->db->where(TERM_FRANCHISE_ID, $fid);
        $this->db->where(TERM_TERM_ID, $tid);
        $res = $this->db->get(TERM.' t');
        if($res->num_rows() > 0){
            $row = $res->row();
            return $row->term.'  '.$row->year;
        }else
            return '';
    }
    
    
    function Generatelist($list, $selected, $admin_id, $details) {
		$CI = & get_instance();
		if ($details) {
			$qry = "SELECT value, description FROM parameter ";
			$qry .= "WHERE type = 'LIST'";
			$qry .= "AND name = '$list'";			
			$qry .= "AND admin_id = ".$admin_id;
			$qry .= " ORDER BY param_id";
		}
		else {
			$qry = "SELECT value FROM parameter ";
			$qry .= "WHERE type = 'LIST'";
			$qry .= "AND name = '$list'";			
			$qry .= "AND admin_id = ".$admin_id;
			$qry .= " ORDER BY param_id";
		}
		
		$res = $CI->db->query($qry)->result();
		$return ='';
		foreach($res as $row) {
			if ($details) {									
				if (strlen($selected) != 0 && $selected === $row->description ) {
					$return .= "<option value=\"" . $row->description . "\" SELECTED>$row->value</option>";
				}
				else {
					$return .= "<option value=\"" . $row->description . "\">$row->value</option>";
				}
			}
			else {
				if (strlen($selected) != 0 && $selected === $row->value ) {
					$return .= "<option value=\"" . $row->value . "\" SELECTED>$row->value</option>";
				}
				else {
					$return .= "<option value=\"" . $row->value . "\">$row->value</option>";
				}
			}			
		}		
			
		// return the new string
		return $return;
	}
    
    
    /*-----------------------------------------------------------------------------------------------
        save_enrolment()
        saves the enrolment details into the enrolment table
-----------------------------------------------------------------------------------------------*/
    function save_enrolment($fid,$cid, $clid, $class, $etype, $chname, $chsurname, $tid, $term_select, $chdob='', $sex='', $mothname='', $fathname='',$street='',$street2='',$pc='',$city='', $phone='', $mobile='', $email='',$medcond='',$comments='',$mktsrc='', $pay_opt='CC', $amtpay='', $cardtype='', $cardname='', $cardnum='', $cardexp='') {
        $CI = & get_instance();
        
        $CI->load->module_model('enrolment.enrolment_model');
        $eid = $CI->enrolment_model->get_last_id();

        $newdob = $chdob;

        $dte = date('Y-m-d');
        if ($etype=='pmt') {
            $data = array(ENROLMENT_FRANCHISE_ID => $fid,
                          ENROLMENT_ENROL_ID => $eid,
                          ENROLMENT_ENROL_TYPE => $etype,
                          ENROLMENT_NAME => $chname,
                          ENROLMENT_SURNAME => $chsurname,
                          ENROLMENT_DATE => $dte,
                          ENROLMENT_AMOUNT => $amtpay,
                          ENROLMENT_PAY_OPT => $pay_opt,
                          ENROLMENT_AMOUNT => $amtpay, 
                          ENROLMENT_CARD_TYPE => $cardtype,
                          ENROLMENT_CARD_NAME => $cardname,
                          ENROLMENT_CARD_NUM => $cardnum,
                          ENROLMENT_CARD_EXP => $cardexp);
        }else {
            $data = array(ENROLMENT_FRANCHISE_ID => $fid,
                          ENROLMENT_ENROL_ID => $eid,
                          ENROLMENT_COURSE_ID => $cid,
                          ENROLMENT_CLASS_ID => $clid,
                          ENROLMENT_TERM_ID => $tid,
                          ENROLMENT_CLASS_NAME => $class,
                          ENROLMENT_ENROL_TYPE => $etype,
                          ENROLMENT_NAME => $chname,
                          ENROLMENT_SURNAME => $chsurname,
                          ENROLMENT_DOB => $newdob,
                          ENROLMENT_SEX => $sex,
                          ENROLMENT_MOTHER => $mothname,
                          ENROLMENT_FATHER => $fathname,
                          ENROLMENT_STREET => $street,
                          ENROLMENT_STREET2 => $street2,
                          ENROLMENT_POSTAL_CODE => $pc,
                          ENROLMENT_CITY => $city,
                          ENROLMENT_PHONE => $phone,
                          ENROLMENT_MOBILE => $mobile,
                          ENROLMENT_EMAIL => $email,
                          ENROLMENT_MED_COND => $medcond,
                          ENROLMENT_COMMENTS => $comments,
                          ENROLMENT_DATE => $dte,
                          ENROLMENT_MKT_SRC => $mktsrc,
                          ENROLMENT_PAY_OPT => $pay_opt,
                          ENROLMENT_AMOUNT => $amtpay,
                          ENROLMENT_CARD_TYPE => $cardtype,
                          ENROLMENT_CARD_NAME => $cardname,
                          ENROLMENT_CARD_NUM => $cardnum,
                          ENROLMENT_CARD_EXP => $cardexp,
                          ENROLMENT_TERM => $term_select);
        }

        $CI->enrolment_model->save($data);
        return $eid;
    } 
    /*-----------------------------------------------------------------------------------------------
		get_payment_option()
		returns the various payment options
-----------------------------------------------------------------------------------------------*/	
	function get_payment_option($admin_id, $option) {		

		if ($option == "CA") {
			$qry = "SELECT cash_txt as txt FROM payment_option where admin_id = ".$admin_id." and cash_opt='Y'";
		}
		if ($option == "CH") {
			$qry = "SELECT cheque_txt as txt FROM payment_option where admin_id = ".$admin_id." and cheque_opt='Y'";
		}
		if ($option == "BT") {
			$qry = "SELECT bank_txt as txt FROM payment_option where admin_id = ".$admin_id." and bank_opt='Y'";
		}
		if ($option == "CC") {
			$qry = "SELECT cc_txt as txt FROM payment_option where admin_id = ".$admin_id." and cc_opt='Y'";
		}
		if ($option == "PP") {
			$qry = "SELECT paypal_txt as txt FROM payment_option where admin_id = ".$admin_id." and paypal_opt='Y'";
		}
		$CI = & get_instance();
        
        
		$res = $CI->db->query($qry);
		$num_rows = $res->num_rows();

		if ($num_rows > 0) {
			$row = $res->row_array();
			
			$return=$row['txt'];
		}
		else {
			$return="NO";
		}
		
		return $return;
	}
	
	function get_payment_option_customer_id($admin_id) {		
		
		$qry = "SELECT cust_acc_id as txt FROM payment_option where admin_id = ".$admin_id;
		$CI = & get_instance();		
		$res = $CI->db->query($qry);
		$num_rows = $res->num_rows();

		if ($num_rows > 0) {
			$row = $res->row_array();
			
			$return=$row['txt'];
		}else{
			$return="NO";
		}

		return $return;
	}
    
    function get_payment_option_paypal_id($admin_id) {
		
		$qry = "SELECT paypal_acc_id as txt FROM payment_option where admin_id = ".$admin_id;
		$CI = & get_instance();
		$res = $CI->db->query($qry);
		$num_rows = $res->num_rows();

		if ($num_rows > 0) {
			$row = $res->row_array();
			
			$return=$row['txt'];
		}else{
			$return="NO";
		}
		return $return;
	}
    
    
    // Get Paymnet OK text
    function get_payment_ok($admin_id, $enrol_id, $enrol_type){
        $qry = "SELECT payment_ok_txt as txt FROM payment_option where admin_id = ".$admin_id;
		$CI = & get_instance();
		$res = $CI->db->query($qry);
		$num_rows = $res->num_rows();

		if ($num_rows > 0) {
			$row = $res->row_array();
			$tmp_return=$row['txt'];
		}
		else
			$tmp_return="";

        $tmps = explode('||@||', $tmp_return);
        $return = '';
        if ($enrol_type=="enrol")
            $return = @$tmps[0];
        if ($enrol_type=="reenrol")
            $return = @$tmps[1];

        if ($enrol_type=="enrol") { 
            $serverName = $_SERVER['SERVER_NAME'];
            $serverPort = $_SERVER['SERVER_PORT'];
			$return.=<<<EOF
		  	<p><strong>Please click <a class='relatedLinkSmall' target='_blank' href=http://$serverName:$serverPort?fid=$admin_id&eid=$enrol_id&mod=memberform>here</a> to view and print your enrolment form. This form is to be signed and returned to the centre in the first week.</strong></p>
EOF;
		}elseif($enrol_type=="pmt"){
			$return.="&nbsp;";
		}
        
		return $return;
    }
}
?>